﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Aspose.Cells;
using Aspose.Cells.Rendering;
using BookingEngine.Data.Fog;
using BookingEngine.Data.Fog.Booking;
using BookingEngine.ExcelService;
using loginRQ = BookingEngine.Data.Fog.loginRQ;

namespace BookingEngine
{
    public partial class MainForm : Form
    {
        private HotelSearchWebServiceClient _searchServiceClient;
        private HotelBookingWebServiceClient _bookingServiceClient;
        private loginRQ _fogLogin;
        private Data.Fog.Booking.loginRQ _bookingLogin;

        public MainForm()
        {
            InitializeComponent();
        }

        private void SubmitButtonClick(object sender, EventArgs e)
        {
            try
            {
                //_fogLogin = new loginRQ
                //                {
                //                    key = "4c7fe919e5f2fbdd8e9cda788d2499fd",
                //                    lang = "1",
                //                    user = "HTKXML"
                //                };
                //_bookingLogin = new Data.Fog.Booking.loginRQ
                //                    {
                //                        key = "4c7fe919e5f2fbdd8e9cda788d2499fd",
                //                        lang = "1",
                //                        user = "HTKXML"
                //                    };
                //using (_searchServiceClient = new HotelSearchWebServiceClient())
                //{
                //    var hotelDetail = _searchServiceClient.getValidHotelByHotelId(_fogLogin, "2011-12-10", "2011-12-11", "6299", "", "T", 1.0);
                //    _responseTextBox.Text = hotelDetail.ToString();
                //}
                //using (_bookingServiceClient = new HotelBookingWebServiceClient())
                //{
                //    var bookingRateRulesRS = _bookingServiceClient.getBookingRateAndRules(_bookingLogin, "6299", "2011-12-10", "2011-12-12", "DQ-BAR", 2, 1, string.Empty, "19:00");
                //    _responseTextBox.Text = bookingRateRulesRS.bookTotal.ToString();
                //}
                //NotificationService.SendMail("Test Mail", "OK", "", new List<string>{"hongliyu2002@hotmail.com", "irlhong@hubs1.net"});
                //NotificationService.SendSms("亲爱的威威小姐，恭喜您获得随心所欲大奖一份，请于2012年1月23日前找 大叔和三叔 兑奖！", "OK", new List<string>{"13501818779", "13816468711"});

                using (var converterClient = new ExcelConverterClient())
                {
                    var fileStream = new FileStream(@"C:\Documents and Settings\irlhong\My Documents\Downloads\accountingv2\CRSGroup.xls", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    var buffer = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, (int) fileStream.Length);

                    //var imageBuffer = converterClient.ConvertToTiff(buffer);
                    //File.WriteAllBytes(@"C:\Documents and Settings\irlhong\My Documents\Downloads\accountingv2\1054.tif", imageBuffer);

                    var pdfBuffer = converterClient.ConvertToPdf(buffer, true);
                    //var pdfBuffer = ConvertToPdf(buffer);
                    File.WriteAllBytes(@"C:\Documents and Settings\irlhong\My Documents\Downloads\accountingv2\CRSGroup.pdf", pdfBuffer);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public byte[] ConvertToPdf(byte[] excelFile)
        {
            var excelFileStream = new MemoryStream(excelFile);
            var workBook = new Workbook(excelFileStream);

            var guid = Guid.NewGuid();
            var path = string.Format(@"C:\Pdk\Temp\Input\{0}.pdf", guid);
            var outputPath = string.Format(@"C:\Pdk\Temp\Output\{0}.pdf", guid);
            var saveOptions = new PdfSaveOptions(SaveFormat.Pdf)
                                  {
                                      Compliance = PdfCompliance.None,
                                      OnePagePerSheet = false
                                  };
            workBook.Save(path, saveOptions);
            var process = new Process
                              {
                                  StartInfo = new ProcessStartInfo(@"C:\Pdk\pdftk.exe", string.Format("\"{0}\" cat 1-endE output \"{1}\"", path, outputPath))
                                                  {
                                                      UseShellExecute = false
                                                  }
                              };
            process.Start();
            process.WaitForExit();
            var newPdfStream = new FileStream(outputPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            var pdfBuffer = new byte[newPdfStream.Length];
            newPdfStream.Read(pdfBuffer, 0, (int) newPdfStream.Length);
            return pdfBuffer;
        }
    }
}